import java.io.*;
import java.util.Arrays;

class ExerciseTwo {
    class ArrayTest {
        private int[] array;

        public void getArrayFromInput() throws IOException {
            try {
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter your integer list (separate with non-digital character):");
                String rowInputedString = stdin.readLine();

                String integerListString = rowInputedString.replaceAll("\\D+", " ");
                String[] splitedIntegerList = integerListString.split(" ");
                array = new int[splitedIntegerList.length];
                for (int i = 0; i < splitedIntegerList.length; ++ i) {
                    array[i] = Integer.parseInt(splitedIntegerList[i]);
                }

                printWithMessage("What you have inputed is:");
            } catch (IOException e) {
                System.out.println("Standard Input Error!");
                throw e;
            }
        }

        public void sort() {
            Arrays.sort(array);
            printWithMessage("Array after sort:");
        }

        public void binarySearch(int target) {
            int position = Arrays.binarySearch(array, target);
            //int start = 0, end = array.length - 1, position = -1;
            //while (start <= end) {
            //    int midPosition = (start + end) / 2;
            //    if (target == array[midPosition]) {
            //        position = midPosition;
            //        break;
            //    } else if (target > array[midPosition]) {
            //        start = midPosition + 1;
            //    } else {
            //        end = midPosition - 1;
            //    }
            //}

            if (position < 0) {
                System.out.print("\nTarget number " + String.valueOf(target) + " does not exist in the list!\n");
            } else {
                System.out.print("\nThe position of target number " + String.valueOf(target) + " is: " + String.valueOf(position) + "\n");
            }
        }

        public void copyFrom(ArrayTest sourceArray) {
            int arrayLength = sourceArray.getArray().length;
            array = Arrays.copyOf(sourceArray.getArray(), arrayLength);
            //array = new int[arrayLength];
            //for (int i = 0; i < arrayLength; ++ i) {
            //    array[i] = sourceArray.getArray()[i];
            //}
            printWithMessage("Array copied:");
        }

        public void fillWithBiggest() {
            int biggest = array[array.length - 1];
            System.out.print("\nThe biggest element is: " + String.valueOf(biggest));
            Arrays.fill(array, biggest);
            //for (int i = 0; i < array.length; ++ i) {
            //    array[i] = biggest;
            //}
            printWithMessage("After filled with the biggest element:");
        }

        public int[] getArray() {
            return array;
        }

        private void printWithMessage(String messageAboveArray) {
            System.out.print("\n" + messageAboveArray + "\n");
            int arrayLength = array.length;
            for (int i = 0; i < arrayLength - 1; ++ i) {
                System.out.print(String.valueOf(array[i]) + ", ");
            }
            System.out.print(String.valueOf(array[arrayLength - 1]) + "\n");
        }
    }

    static public void main(String args[]) {
        ExerciseTwo test = new ExerciseTwo();
        test.doTest();
    }

    public void doTest() {
        try {
            ArrayTest test1 = new ArrayTest();
            test1.getArrayFromInput();
            test1.sort();
            for (int element : test1.getArray()) {
                test1.binarySearch(element);
            }
            ArrayTest test2 = new ArrayTest();
            test2.copyFrom(test1);
            test2.fillWithBiggest();
        } catch (Exception e) {
            System.out.println("Application teminated abnormally.");
            e.printStackTrace();
        }
    }
}
